package
{

    import flash.display.Sprite;
    import flash.events.Event;

    
    public class ParticleEmmiter extends Sprite
    {

            //-------------------------- CONFIGURATION --------------------------//
            public var minGen:uint = 1;
            public var maxGen:uint = 10;
            public var maxParticles:uint = 60;
            public var xVelocity:Number = 0;
            public var xSpread:Number = 4;
            public var yVelocity:Number = -20;
            public var ySpread:Number = 4;
            public var beginColor:Number = 0x000000;
            public var endColor:Number = 0xFF0000;
            
            public var lifetime:Number = 30;
            public var create:Boolean = false;
            
                
            //-------------------------- INTERNAL --------------------------//
            private var generateTick:uint = 1;
            private var currentTick:uint = 0;
            private var gravity:Number = 0.9;
            private var refreshTick:int = 1;
            private var particles:Array = [];
            
            public function ParticleEmmiter()
            {
                addEventListener(Event.ENTER_FRAME, onEnterFrame);
            }
            
            
            public function start():void 
            {
                create = true;

            } 
            
            public function stop():void
            {
                create = false;

            }
            
            public function onEnterFrame(event:Event) :void
            {

                if (currentTick++>generateTick) 
                {
                    currentTick = 0;
                }
                
                if (currentTick ==0  && particles.length<maxParticles && create) {
                
                    for (var i:uint = 0; i<minGen+Math.random()*maxGen; i++) {
                        var particle:Object = {};
                        particle.x = 10;
                        particle.y = 10;
                        particle.vx = xVelocity+ -xSpread/2+Math.random()*xSpread;
                        particle.vy =  yVelocity * ySpread/2+Math.random()*ySpread;
                        particle.lifetime = lifetime;

                        particles.push(particle);
                        
                        if (particles.length>=maxParticles)
                            break;
                    }
                }
                

                
                if (refreshTick--<0) {
                    
                  for each (var object:Object in particles)
                  {
                      object.x +=object.vx;
                      object.y +=object.vy;
                      object.vy+=gravity;                  
                      object.lifetime --;
                      
                      if (object.lifetime < 1) {
                        particles.shift();
                      }
                  }
                  
                  updateGraphics();
                  refreshTick = 2;
                }
                
            }
            
            private function updateGraphics():void
            {

                this.graphics.clear();
                for each (var object:Object in particles)
                {
                    
                    var color:Number = interpolateColor(endColor,beginColor, object.lifetime/lifetime);
                    
                    this.graphics.beginFill(color);
                    this.graphics.drawRect(object.x,object.y,3, 3);
                }
            }
            
            public static function interpolateColor(hex1:uint, hex2:uint, percent:Number):uint { 
                // percent is between 0 and 1 
                // 
                var alpha1:uint = hex1 >> 24 & 0xFF; 
                var red1:uint = hex1 >> 16 & 0xFF; 
                var green1:uint = hex1 >> 8 & 0xFF; 
                var blue1:uint = hex1 & 0xFF; 
                // 
                var alpha2:uint = hex2 >> 24 & 0xFF; 
                var red2:uint = hex2 >> 16 & 0xFF; 
                var green2:uint = hex2 >> 8 & 0xFF; 
                var blue2:uint = hex2 & 0xFF; 
                // 
                var alpha3:uint = alpha1+((alpha2-alpha1)*percent) 
                var red3:uint = red1+((red2-red1)*percent) 
                var green3:uint = green1+((green2-green1)*percent) 
                var blue3:uint = blue1+((blue2-blue1)*percent) 
                // 
                var a:String = alpha3.toString(16); 
                if (a.length==1){ 
                    a = "0"+a 
                } 
                var r:String=red3.toString(16); 
                if (r.length==1){ 
                    r = "0"+r 
                } 
                var g:String=green3.toString(16); 
                if (g.length==1){ 
                    g = "0"+g 
                } 
                var b:String=blue3.toString(16); 
                if (b.length==1){ 
                    b = "0"+b 
                } 
                // 
                var combined:String = '0x'+a+r+g+b 
                // 
                var hex3:uint = Number(combined) 
                // 
                return(hex3) 
                // 
            }; 
    }
}

